/**
 * 系统用户管理页面js
 * 版权所有：上海戈品投资管理有限公司
 *   author king
     功能说明：系统用户管理页面
     创建日期：2015-11-17
     修改记录：
     备注说明：
 */

$(document).ready(function() {
	userList();
});

/**
 * 查询资源列表
 * @returns
 */
var userList = function(){
	
	var datas = {"p":"sysUserManage","m":"list" };//请求参数
	var columnDefine = {"id":"ID","name":"名称","roleName":"角色"};//表格栏目定义
	var formatters = {};//自定义表格格式
	var buttons =''; //操作按钮
	buttons+='<p>';
	buttons+='<button type=\'button\' class=\'btn btn-w-m btn-info\' onclick="showModifyWin();">编辑</button>&nbsp;&nbsp;';
	buttons+='<button type=\'button\' class=\'btn btn-w-m btn-danger\' onclick="openSetRoleWin();">设置角色</button>';
	buttons+='</p>';
	hui.pager('service',datas ,"dataList", columnDefine , formatters , buttons ); //分页查询
};



/**
 * 打开新建窗口
 * @returns
 */
var showNewWin = function(){
	$('#newPop input').val('');
	$('#newPop').modal('show');
};


/**
 * 打开设置权限窗口
 * @returns
 */
var openSetRoleWin = function(){
	var data = $('#pop_win .data').text();
	var m = eval('('+data+')');
	$('#pop_win').modal('hide');
	var options = '';
	
	var json = common.ajaxRequest('service', {"p":"roleManage","m":"list" } , 'POST');
	$(json.roleList).each(function(){
		options+='<option value="'+this.id+'">'+this.name+'</option>';
	});
	
	$('#setRolePop .userId').val(m.id);
	$('#setRolePop .roleId').empty();
	$('#setRolePop .roleId').append(options);
	$('#setRolePop').modal('show');
};

/**
 * 设置角色
 * @returns
 */
var setRole = function(){
	var userId = $('#setRolePop .userId').val();
	var roleId = $('#setRolePop .roleId').val();
	
	var datas = {"p":"userRoleManage","m":"new","userId":userId ,"roleId":roleId };
	var json = common.ajaxRequest('service', datas , 'POST');
	if( json && json.result == 0 )
	{
		userList();
		$('#setRolePop').modal('hide');
		common.showMsg('info', '', '设置成功!');
	}
	else
	{
		common.showMsg('error', '设置失败', json.message);
	}	
	
};

/**
 * 打开编辑窗口
 * @returns
 */
var showModifyWin = function(obj){
	var data = $('#pop_win .data').text();
	var json = eval('('+data+')');
	$('#editPop .id').val(json.id);
	$('#editPop .name').val(json.name);
	$('#editPop .username').val(json.username);
	//$('#editPop .password').val(json.password);
	$('#pop_win').modal('hide');
	$('#editPop').modal('show');
};

/**
 * 新建用户
 * @returns
 */
var newUser = function(){

	var name = $('#newPop .name').val();
	var username = $('#newPop .username').val();
	var password = $('#newPop .password').val();
	var datas = {"p":"sysUserManage","m":"new","name":name ,"username":username,"password":password};
	var json = common.ajaxRequest('service', datas , 'POST');
	if( json && json.result == 0 )
	{
		userList();
		$('#newPop').modal('hide');
		common.showMsg('info', '', '新建成功!');
	}
	else
	{
		common.showMsg('error', '创建失败', json.message);
	}	
};

/**
 * 编辑账号
 * @returns
 */
var modifyUser = function(){
	
	var id = $('#editPop .id').val();
	var name = $('#editPop .name').val();
	var username = $('#editPop .username').val();
	var password = $('#editPop .password').val();
	var status = 0;
	var datas = {"p":"sysUserManage","m":"modify","name":name,"id":id,"username":username,"status":0,"password":password};
	var json = common.ajaxRequest('service', datas , 'POST');
	if( json && json.result == 0 )
	{
		userList();
		$('#editPop').modal('hide');
		common.showMsg('info', '', '修改成功');
	}
	else
	{
		common.showMsg('error', '修改成功', json.message);
	}		
};

